package com.wj.algorithm.lintcode;

class ListNode {
    int val;
    ListNode next;

    ListNode(int x) {
        val = x;
    }
}

public class P452 {

    public ListNode removeElements(ListNode head, int val) {
        
        if(head == null) {
            return null;
        }
        ListNode rsHead = new ListNode(-1);
        ListNode current = rsHead;
        
        ListNode scan = head; 
        while(scan != null) {
            if(scan.val != val) {
                current.next = scan;
                current = current.next;
            }
            scan = scan.next;
        }
        if(current.next != null && current.next.val == val) {
            current.next = null;
        }
        
        return rsHead.next;
    }
}
